# _*_ coding:utf-8 _*_
"""
@author:lx
@file: split
@contact: lixiang-929@outlook.com
@time: 2019/03/16
"""
import cv2
import numpy as np
import os
import time

data_path = '.\\data\\'
result_path = '.\\result\\'

def color_Segement(data_path,result_path):
    data_list = os.listdir(data_path)
    for data in data_list:
        start_time = time.time()
        image_name = os.path.join(data_path,data)
        image = cv2.imread(image_name)
        image_width = image.shape[1]
        image_height = image.shape[0]

        rg_image = np.zeros([image_height,image_width])
        gb_image = np.zeros([image_height,image_width])
        rb_image = np.zeros([image_height,image_width])

        red_image = np.zeros([image_height,image_width])
        yellow_image = np.zeros([image_height,image_width])

        b_channel,g_channel,r_channel = cv2.split(image)

        for j in range(image_height):
            for i in range(image_width):
                if r_channel[j,i] > g_channel[j,i]:
                    rg_image[j,i] = r_channel[j,i] - g_channel[j,i]
                else:
                    rg_image[j,i] = 0
                if g_channel[j,i] > b_channel[j,i]:
                    gb_image[j,i] = g_channel[j,i] - b_channel[j,i]
                else:
                    gb_image[j,i] = 0
                # if r_channel[j,i] > b_channel[j,i]:
                #     rb_image[j,i] = r_channel[j,i] - b_channel[j,i]
                # else:
                #     rb_image[j,i] = 0

        for j in range(image_height):
            for i in range(image_width):
                if rg_image[j,i] > gb_image[j,i]:
                    red_image[j,i] = rg_image[j,i] - gb_image[j,i]
                else:
                    yellow_image[j,i] = gb_image[j,i] - rg_image[j,i]


        file_name,file_format = os.path.splitext(data)
        red_image_name = os.path.join(result_path,file_name + '_red_image.jpg')
        yellow_image_name = os.path.join(result_path,file_name + '_yellow_image.jpg')
        cv2.imwrite(red_image_name,red_image)
        cv2.imwrite(yellow_image_name,yellow_image)
        print("{0:.3f},{1:s}".format((time.time() - start_time),file_name))
        cv2.waitKey(10)

if __name__ == '__main__':
    color_Segement(data_path,result_path)



